package zhklappoint.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import zhklappoint.dao.FunctionKindsDao;
import zhklappoint.entity.FunctionKinds;
import zhklappoint.enums.ENUM_ISDELETE;
import zhklappoint.enums.ENUM_ISUSING;
import zhklappoint.service.FunctionKindsService;
import zhklappoint.util.PageUtils;

@Service
public class FunctionKindsServiceImpl implements FunctionKindsService{

	@Autowired
	private FunctionKindsDao fkd;
	
	@Override
	public FunctionKinds saveFunctionKinds(FunctionKinds fk) {
		return fkd.save(fk);
	}

	@Override
	public Page<FunctionKinds> getFunctionKindsList(FunctionKinds functionKinds, Integer page, Integer size) {
		functionKinds.setIsdelete(ENUM_ISDELETE.NOT_DELETED.value);
        Pageable pageable= PageUtils.toPageable(page, size, Sort.Direction.DESC, "createdate");
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withMatcher("isdelete", ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("code", ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("status", ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("id", ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
                .withIgnorePaths("createdate","updatedate");// 忽略字段，即不管忽略名字是什么值都不加入查询条件
        Example<FunctionKinds> example = Example.of(functionKinds, matcher);
        return fkd.findAll(example, pageable);
	}

	@Transactional
	@Override
	public void deleteAllFunctionKindsList() {
		 String sql="update zhkl_appoint_function set isdelete="+ENUM_ISDELETE.DELETED.value;
		 fkd.sqlQuery(sql);
	}

	@Override
	public Page<FunctionKinds> getCompanyHasFuncion(String cid,Integer page,Integer size) {
		FunctionKinds fkkkk=new FunctionKinds();
		Pageable pageable = PageUtils.toPageable(page, size, Sort.Direction.DESC, "createdate");
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("isdelete", ENUM_ISDELETE.NOT_DELETED.value);
		values.put("cid", cid);
		StringBuffer queryString = new StringBuffer("select zaf.* from zhkl_appoint_function zaf left join zhkl_function_company zfc on zaf.id = zfc.fk_id");
		queryString.append(" WHERE zaf.isdelete = :isdelete ");
		queryString.append(" and zfc.isdelete = :isdelete");
		queryString.append(" and zfc.company_id=:cid");
		queryString.append(" and zfc.status="+ENUM_ISUSING.USING.value);
		queryString.append(" ORDER BY createdate DESC");
		StringBuffer countString = new StringBuffer("select count(1) from (").append(queryString).append(") a");
		return fkd.sqlQuery(queryString+"", countString+"", values, pageable,fkkkk.getClass());
	}

	@Override
	public FunctionKinds getFunctionKindsById(String id) {
		 Optional<FunctionKinds> ou=fkd.findById(id);
	        if(!ou.isPresent()) {
	            return null;
	        }
	        FunctionKinds c=ou.get();
	        if(c.getIsdelete()==ENUM_ISDELETE.DELETED.value){
	            return null;
	        }
	        return c;
	}
}
